import { Component, OnInit } from '@angular/core';
import { AddConShareManageComponent } from '../add-con-share-manage.component';
import { ErrRes } from '../../../../../frame/global/err-res';
import { ConShareManage } from '../../con-share-manage.type';
import { Services } from '../../../../../frame/module/services';
import { HttpRequestClient } from '../../../../../frame/http-request-client/http-request-client';
import { UrlRou } from '../../../../../frame';
import { Zero3Alert } from '../../../../../frame/global/zero3-alert/zero3-alert.component';
import { Zero3Enum } from '../../../../../frame/enum/zero3-enum';
import * as fa from '@fortawesome/free-solid-svg-icons';
import { FroalaEditor } from '../../../../../frame/global/froala-editor';
import { Service } from 'protobufjs';
import { GooGoodsCondition } from '../../../../goods-management/goo-goods/goo-goods.type';
import { isNullOrUndefined } from 'util';
import { SocArticleCondition } from '../../../../social-management/soc-article/soc-article.type';
import { DraIntegralLotteryCondition } from '../../../../draw-management/dra-integral-lottery/dra-integral-lottery.type';
import { DraFreeLotteryCondition } from '../../../../draw-management/dra-free-lottery/dra-free-lottery.type';

@Component({
    selector: 'app-main-add-con-share-manage',
    templateUrl: './main-add-con-share-manage.component.html',
    styleUrls: ['./main-add-con-share-manage.component.css']
})
export class MainAddConShareManageComponent implements OnInit {

    froalaEditorOptions = Object.assign(FroalaEditor.options, { enter: "div" });

    errRes = new ErrRes();

    domain = new ConShareManage();

    extend = {
        domain: {
            serShareType: {
                items: null,
                nickname: "分享类型",

                initItems: () => {
                    this.extend.domain.serShareType.items = [];
                    Zero3Enum.SHARE_FLAG.forEach((val, key) => {
                        this.extend.domain.serShareType.items.push({
                            name: val,
                            id: key,
                        }
                        )
                    })
                },
                change: (event) => {
                    let that = this.extend.domain.fkUUID;
                    //关联商品
                    if (event.id == Zero3Enum.CON_SHARE_MANAGE_TYPE_GOODS) {
                        Services.gooGoods.list(new GooGoodsCondition(),
                            {
                                page: 0,
                                size: 9999,
                                orders: 'createTime-desc,serNum-asc'
                            }
                        ).subscribe(res => {
                            if (isNullOrUndefined(res) || res.code !== 'success') {
                                return;
                            }
                            that.items = [];
                            res.page.content.map(item => {
                                that.items.push({
                                    id: item.uuid,
                                    name: item.serGoodsName
                                });
                            })
                        })
                    }
                    //关联活动
                    if (event.id == Zero3Enum.CON_SHARE_MANAGE_TYPE_ACTIVITY) {
                        // 免费抽奖商品
                        let tmp = [];
                        let isFinish1 = false, isFinish2 = false;
                        Services.draFreeLottery.list(new DraFreeLotteryCondition(), {
                            page: 0,
                            size: 999,
                            orders: 'createTime-desc,serNum-asc'
                        }).subscribe(data => {
                            if (!isNullOrUndefined(data) && data.code === 'success') {
                                data.page.content.forEach(item => {
                                    tmp.push({
                                        id: item.uuid, name: item.serGoodsDrawPname
                                    });
                                });
                                isFinish1 = true;
                                if (isFinish2) {
                                    that.items = tmp;
                                }
                            }
                        });
                        // 积分抽奖商品
                        Services.draIntegralLottery.list(new DraIntegralLotteryCondition(), {
                            page: 0,
                            size: 999,
                            orders: 'createTime-desc,serNum-asc'
                        }).subscribe(data => {
                            if (!isNullOrUndefined(data) && data.code === 'success') {
                                data.page.content.forEach(item => {
                                    tmp.push({
                                        id: item.uuid, name: item.serGoodsDrawName
                                    });
                                });
                                isFinish2 = true;
                                if (isFinish1) {
                                    that.items = tmp;
                                }
                            }
                            });
                    }
                    if (event.id == Zero3Enum.CON_SHARE_MANAGE_TYPE_ARTICLE) {
                        // 文章
                        Services.socArticle.list(new SocArticleCondition(), {
                            page: 0,
                            size: 999,
                            orders: 'createTime-desc,serNum-asc'
                        }).subscribe(item => {
                            if (!isNullOrUndefined(item) && item.code === 'success') {
                                this.extend.domain.fkUUID.items = [];
                                item.page.content.forEach(item2 => {
                                    this.extend.domain.fkUUID.items.push({
                                        id: item2.uuid, name: item2.serTheme
                                    });
                                });
                            }

                        });
                    }
                }
            },
            fkUUID: {
                nickname: "关联对象名称",

                items: [],
                change: (event) => {
                    this.domain.serFkName = event.name;
                }
            },
            default: () => {
                this.domain.serAudit = Zero3Enum.AUDIT_FLAG_WAIT_SUBMIT;
                this.domain.serUsing = Zero3Enum.ENABLE;
                this.extend.domain.serAudit.nickname = Zero3Enum.AUDIT_FLAG.get(this.domain.serAudit);
            },
            serAudit: {
                nickname: ''
            },
            file: {
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.file.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.file.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serFileUrl = this.extend.domain.file.oss.fileUrl;
                        } else {
                            this.extend.domain.file.oss.fileUrl = '';
                            this.domain.serFileUrl = this.extend.domain.file.oss.fileUrl;
                            Zero3Alert.req('fail');
                        }
                    });
                }
            }
        },
    };

    func = {
        save: {
            req: (event) => {
                Services.conShareManage.save(this.domain, this.func.save.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    HttpRequestClient.router.navigate([UrlRou.conShareManage.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        }
    };

    constructor(public addConShareManage: AddConShareManageComponent) {
        this.addConShareManage.main = this;
    }

    ngOnInit() {
        this.extend.domain.default();
        this.extend.domain.serShareType.initItems();
    }


}
